/*
 * comlan
 */

package com.promfy.timetable.ui;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.promfy.timetable.R;
import com.promfy.timetable.db.ReminderDBService;
import com.promfy.timetable.domain.Reminders;
import com.promfy.util.DateFormatUtil;

public class ReminderListView extends LinearLayout implements OnClickListener{

	
	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			int what = msg.what;
			switch (what) {
			
			default:
				break;
			}
			super.handleMessage(msg);
		}
		
	};
	
	private LayoutInflater inflater;

	private Context context;

	private View view;

	private ListView listView;

	private List<Reminders> reminderList = new ArrayList<Reminders>();

	private BaseAdapter adapter;

	private Button addBtn;
	
	public ReminderListView(Context context) {
		super(context);
		this.context = context;
	
		initView();
		initData();
	}

	public ReminderListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	private void initView(){
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.reminders, this);
		listView = (ListView)view.findViewById(R.id.listview_reminders);
		addBtn = (Button)view.findViewById(R.id.add_reminders);
		adapter = new ReminderListViewAdapter(context, 0, R.layout.reminders_listview_item, reminderList);
		listView.setAdapter(adapter);
		
		addBtn.setOnClickListener(this);
	} 
	
	public void initData() {
//		Reminders r1 = new Reminders(System.currentTimeMillis(), "aaa", Reminders.REMINDERS_STATUS_INIT);
//		Reminders r2 = new Reminders(System.currentTimeMillis(), "bbb", Reminders.REMINDERS_STATUS_INIT);
//		Reminders r3 = new Reminders(System.currentTimeMillis(), "ccc", Reminders.REMINDERS_STATUS_INIT);
//		reminderList.add(r1);
//		reminderList.add(r2);
//		reminderList.add(r3);
		ReminderDBService rds = new ReminderDBService(context);
		try {
			List<Reminders> reminders = rds.getList();
			reminderList.clear();
			reminderList.addAll(reminders);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//restart
		case R.id.add_reminders:
			
			break;
		//back to mode
		case 2:

			break;
		//exit
		case 3:
			
			break;
		default:
			break;
		}
	}

	
	private class ReminderListViewAdapter extends ArrayAdapter {

		private LayoutInflater inflater;
		private ArrayList<Reminders> reminders;

		private Bitmap collapseIcon;
		private Bitmap expandIcon;

		public ReminderListViewAdapter(Context context, int resource, int textViewResourceId, List objects) {
			super(context, resource, textViewResourceId, objects);
			inflater = LayoutInflater.from(context);
			reminders = (ArrayList<Reminders>) objects;

		}

		@Override
		public Object getItem(int position) {
			return reminders.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.reminders_listview_item, null);

				holder = new ViewHolder();
				holder.title = (TextView) convertView.findViewById(R.id.title_reminders_listview_item);
				holder.content = (TextView) convertView.findViewById(R.id.content_reminders_listview_item);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			try {

				Reminders reminder = (Reminders) getItem(position);
				holder.title.setText(DateFormatUtil.formatDate(reminder.getTime()));
				holder.content.setText(reminder.getContent());
				
			} catch (Exception e) {
				e.printStackTrace();
			}

			return convertView;
		}

		class ViewHolder {
			TextView title;
			TextView content;
		}

		@Override
		public int getCount() {
			return reminders.size();
		}

	}


}
